package com.lp.common.exception;

import lombok.EqualsAndHashCode;

import static com.lp.common.exception.ErrorCodeEnum.PARAMS_ERROR;

/**
 * @program: backend
 * @description:
 * @author: Ke.Song
 * @since: 2024-03-11 21:15:21
 */
@EqualsAndHashCode(callSuper = true)
public class BaseException extends RuntimeException {

    /**
     * -- GETTER --
     *  Get params object [ ].
     *
     */
    private final Object[] params;

    private ErrorCodeEnum errorCodeEnum;

    private String msg;

    private boolean custom = false;

    /**
     * Instantiates a new Base exception.
     *
     * @param errorCodeEnum the error code enum
     */
    public BaseException(ErrorCodeEnum errorCodeEnum) {
        this.errorCodeEnum = errorCodeEnum;
        this.params = new Object[]{};
    }

    /**
     * Instantiates a new Base exception.
     *
     * @param msg 异常消息
     */
    public BaseException(String msg) {
        this.msg = msg;
        this.params = new Object[]{};
        this.custom = true;
    }

    /**
     * Instantiates a new Base exception.
     *
     * @param errorCodeEnum the error code enum
     * @param param         the param
     */
    public BaseException(ErrorCodeEnum errorCodeEnum, Object... param) {
        this.errorCodeEnum = errorCodeEnum;
        this.params = param;
    }

    public String getMessage(boolean isProd) {
        this.msg = this.custom ? this.msg : this.errorCodeEnum.getMsg();
        return this.msg;
    }

    /**
     * Get biz code string.
     *
     * @return the string
     */
    public Integer getBizCode(){
        return this.custom ? 400 : this.errorCodeEnum.getBizCode();
    }

}


